import React from 'react';
import ReactDOM from 'react-dom';
import { createBrowserHistory } from 'history';
import { createStore, compose, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import createSagaMiddleware from 'redux-saga';
import { routerMiddleware } from 'connected-react-router';
/* 系统唯一的store，通过Provider把它传递下去, 让子组件能够拿到store对象 */
import createRootReducer from '@/reducers';
import mySaga from '@/sagas';
import AppRouter from '@/router';

import './styles/global.less';
import 'antd/dist/antd.less';

const history = createBrowserHistory();
const sagaMiddleware = createSagaMiddleware();
const finalCreateStore = preloadedState => createStore(
  createRootReducer(history),
  preloadedState,
  compose(
    applyMiddleware(
      routerMiddleware(history),
      sagaMiddleware,
    ),
  ),
);
const store = finalCreateStore();

sagaMiddleware.run(mySaga)

ReactDOM.render(
  <Provider
    store={store}
  >
    <AppRouter
      history={history}
    />
  </Provider>,
  document.getElementById('root'),
)
